{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.append('..')\n",
    "sys.path.append('../')\n",
    "sys.path.append('../convformer/')\n",
    "sys.path\n",
    "import numpy as np\n",
    "import torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<All keys matched successfully>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pathml.datasets.MICCAIds import MICCAIDataSet\n",
    "from pathml.ml import hovernet\n",
    "from pathml.ml.hovernet_attention import HoVerNet as HA, post_process_batch_hovernet\n",
    "from pathml.ml.hovernet import HoVerNet as HC\n",
    "from pathml.ml.hovernet_convformer import HoVerNet_ConvFormer as HCF\n",
    "\n",
    "test_dataset = MICCAIDataSet('../data/MICCAI2018MoNuSeg', transform=None, mode='test')\n",
    "test_dataloader = torch.utils.data.DataLoader(test_dataset, batch_size=1, shuffle=False)\n",
    "device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n",
    "# hovernet1 = HC().to(device)\n",
    "# hovernet1.load_state_dict(torch.load('hovernet_fully_trained1.pt', map_location=device))\n",
    "hovernet2 = HA().to(device)\n",
    "hovernet2.load_state_dict(torch.load('hovernet_att_best_perf.pt', map_location=device))\n",
    "hovernet3 = HCF(n_channels=3, imgsize=512).to(device)\n",
    "hovernet3.load_state_dict(torch.load('hovernet_convformer_fully_trained.pt', map_location=device))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Dice1:  0.6056319352065581   Dice2:  0.6131431683417066\n",
      "HD95_1:  37.08049173025688   HD95_2:  37.44218326595562\n",
      "IOU1:  0.4349612710290452   IOU2:  0.442914710471597\n",
      "Accuracy1:  0.8663738250732422   Accuracy2:  0.8601787567138672\n",
      "Precision1:  0.5461264772790458   Precision2:  0.5353903133987425\n",
      "Recall1:  0.6851942416650412   Recall2:  0.7349206465480519\n"
     ]
    }
   ],
   "source": [
    "import cv2\n",
    "\n",
    "from examples.evaluation_metrics import *\n",
    "# hovernet1.eval()\n",
    "hovernet2.eval()\n",
    "hovernet3.eval()\n",
    "dice1_sum, dice2_sum, hd95_1sum, hd95_2sum, iou1_sum, iou2_sum = 0, 0, 0, 0, 0, 0\n",
    "acc1_sum, acc2_sum, prec1_sum, prec2_sum, recall1_sum, recall2_sum = 0, 0, 0, 0, 0, 0\n",
    "with torch.no_grad():\n",
    "    for idx, data in enumerate(test_dataloader):\n",
    "        images = data[0].float().to(device)\n",
    "        masks = data[1].to(device)\n",
    "        outputs1 = hovernet3(images)\n",
    "        outputs2 = hovernet2(images)\n",
    "        preds_detection1 = post_process_batch_hovernet(outputs1, n_classes=None).squeeze()\n",
    "        preds_detection2 = post_process_batch_hovernet(outputs2, n_classes=None).squeeze()\n",
    "        preds_detection1 = preds_detection1 != 0\n",
    "        preds_detection2 = preds_detection2 != 0\n",
    "        mask = masks.squeeze().cpu().numpy() == 0\n",
    "        dice1_sum += dice_coefficient(mask, preds_detection1)\n",
    "        dice2_sum += dice_coefficient(mask, preds_detection2)\n",
    "        hd95_1sum += calculate_hd95(mask, preds_detection1)\n",
    "        hd95_2sum += calculate_hd95(mask, preds_detection2)\n",
    "        iou1_sum += iou(mask, preds_detection1)\n",
    "        iou2_sum += iou(mask, preds_detection2)\n",
    "        acc1_sum += accuracy(mask, preds_detection1)\n",
    "        acc2_sum += accuracy(mask, preds_detection2)\n",
    "        prec1_sum += precision(mask, preds_detection1)\n",
    "        prec2_sum += precision(mask, preds_detection2)\n",
    "        recall1_sum += recall(mask, preds_detection1)\n",
    "        recall2_sum += recall(mask, preds_detection2)\n",
    "        \n",
    "    print('Dice1: ', dice1_sum / len(test_dataloader), '  Dice2: ', dice2_sum / len(test_dataloader))\n",
    "    print('HD95_1: ', hd95_1sum / len(test_dataloader), '  HD95_2: ', hd95_2sum / len(test_dataloader))\n",
    "    print('IOU1: ', iou1_sum / len(test_dataloader), '  IOU2: ', iou2_sum / len(test_dataloader))\n",
    "    print('Accuracy1: ', acc1_sum / len(test_dataloader), '  Accuracy2: ', acc2_sum / len(test_dataloader))\n",
    "    print('Precision1: ', prec1_sum / len(test_dataloader), '  Precision2: ', prec2_sum / len(test_dataloader))\n",
    "    print('Recall1: ', recall1_sum / len(test_dataloader), '  Recall2: ', recall2_sum / len(test_dataloader))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py38",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
